package com.nidu.demo.oauth2.model;

import cn.hutool.core.util.IdUtil;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.base.BaseDeletableEntity;
import com.nidu.demo.base.BaseTenantEntity;
import com.nidu.demo.common.enums.BooleanEnum;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;
import java.util.List;

/**
 * OAuth2刷新令牌领域对象
 *
 * @author auto
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class OAuth2RefreshToken extends BaseTenantEntity implements BaseDeletableEntity {

    private Long id;

    /**
     * 用户编号
     */
    private Long userId;

    /**
     * 刷新令牌
     */
    private String refreshToken;

    /**
     * 用户类型
     */
    private Integer userType;

    /**
     * 客户端编号
     */
    private Long clientId;

    /**
     * 授权范围
     */
    private List<String> scopes;

    /**
     * 刷新令牌过期时间
     */
    private LocalDateTime expiresTime;

    /**
     * 是否删除
     */
    private Boolean deleted;

    /**
     * 创建refresh token
     * @param userId
     * @param userType
     * @param clientId
     * @param scopes
     * @param expiresTime
     * @return
     */
    public static OAuth2RefreshToken create(Long userId, Integer userType, Long clientId, List<String> scopes, LocalDateTime expiresTime) {
        OAuth2RefreshToken token = new OAuth2RefreshToken();
        token.setUserId(userId)
                .setUserType(userType)
                .setRefreshToken(IdUtil.fastSimpleUUID())
                .setClientId(clientId)
                .setScopes(scopes)
                .setExpiresTime(expiresTime);
        return token;
    }

    /**
     * 更新refresh token
     * @param id
     * @param userId
     * @param userType
     * @param refreshToken
     * @param clientId
     * @param scopes
     * @param expiresTime
     * @return
     */
    public static OAuth2RefreshToken update(Long id, Long userId, Integer userType, String refreshToken, Long clientId, List<String> scopes, LocalDateTime expiresTime) {
        OAuth2RefreshToken token = new OAuth2RefreshToken();
        token.setId(id)
                .setUserId(userId)
                .setUserType(userType)
                .setRefreshToken(refreshToken)
                .setClientId(clientId)
                .setScopes(scopes)
                .setExpiresTime(expiresTime);
        return token;
    }

    @Override
    public void isDeleted() {
        if (BooleanEnum.TRUE.getValue().equals(this.getDeleted())) {
            throw new BizException(ErrorCodeConstants.OAUTH2_REFRESH_TOKEN_NOT_FOUND.getCode(), ErrorCodeConstants.OAUTH2_REFRESH_TOKEN_NOT_FOUND.getMessage());
        }
    }

    public void isExpired(){
        // 检查token是否过期
        if (this.getExpiresTime().isBefore(LocalDateTime.now())){
            throw new BizException(ErrorCodeConstants.REFRESH_TOKEN_EXPIRED.getCode(), ErrorCodeConstants.REFRESH_TOKEN_EXPIRED.getMessage());

        }
    }
}